//
//  HNStudentSignCell.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/6.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNStudentSignCell.h"
#import "AppDelegate.h"

@implementation HNStudentSignCell
{
    __weak IBOutlet UIButton *_State; // 状态按钮
    __weak IBOutlet UIButton *_Select; // 选择按钮
    __weak IBOutlet UIButton *_SelectTwo; // 大选择按钮
    __weak IBOutlet UIImageView *_Img; // 头像
    __weak IBOutlet UILabel *_Name; // 名称
    __weak IBOutlet UILabel *_Num; // 上课次数
    __weak IBOutlet UIButton *_Detail; // 签到详情
    
    UIImageView *deleteImage;
}


- (void)setModel:(HNStudentSignModel *)Model {
    
    _Model = Model;
    
    _SelectTwo.selected = _Select.selected = Model.Selected; // 选中状态
    [_State setTitle:Model.State forState:(UIControlStateNormal)]; // 字符显示
    [_State setBackgroundImage:Model.StateImage forState:(UIControlStateNormal)]; // 状态背景色
    _Name.text = Model.Name; 
    _Num.text = Model.Num;
    ![Model.avatar isKindOfClass:[NSNull class]] ? [_Img sd_setImageWithURL:[NSURL URLWithString:Model.avatar] placeholderImage:[UIImage imageNamed:@"bg_picture_defaultbg_daoke_student_sign"]] : nil;
    
    
    // 状态按钮要不要隐藏
    _State.hidden = [Model.State isEqualToString:@"隐藏"] ? YES:NO;
    // 选中按钮要不要禁点
    NSArray *Arr = [Model.Num componentsSeparatedByString:@"/"];
    NSInteger FirstIndex = [[Arr firstObject] integerValue];
    NSInteger LastIndex = [[Arr lastObject] integerValue];
    _SelectTwo.enabled = _Select.enabled = ![Model.State isEqualToString:@"请假"] && ![Model.State isEqualToString:@"缺勤"] && FirstIndex < LastIndex ? YES:NO;
    
    /// 添加长按删除
    UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)];
    longPressGesture.minimumPressDuration = 1.0f;//设置长按 时间
    [self addGestureRecognizer:longPressGesture];
}


/** 点击了选中 */                              
- (IBAction)ClickItem:(UIButton *)sender {
    
    self.Click ? self.Click() : nil;
}

/** 点击进入了考勤记录 */
- (IBAction)ClickHistory:(UIButton *)sender {
    
    self.ClickHistory ? self.ClickHistory (self.Model.enrollInfoId) : nil;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    
    _Img.layer.cornerRadius = _Img.frame.size.width / 2;
    _Img.layer.masksToBounds = YES;
}

#pragma mark - 长按手势触发
- (void)cellLongPress:(UITapGestureRecognizer *)tap {
//    deleteImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pop_deleted_default_handle_course"]];
//    [deleteImage setUserInteractionEnabled:YES];
//    [self.contentView addSubview:deleteImage];
//
//    [deleteImage mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.right.equalTo(self.contentView);
//    }];
//
//    UITapGestureRecognizer *tapCilk = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteStudent:)];
//    [deleteImage addGestureRecognizer:tapCilk];
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否删除选中的学生" preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        self.ClickDelete ? self.ClickDelete(self.Model.enrollInfoId,self.Model.User_ID) : nil;
    }];
    
    UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
//        if (_ReloadData) {
//            _ReloadData();
//            [deleteImage removeFromSuperview];
//        }
    }];
    
    [alertVC addAction:actionOne];
    [alertVC addAction:actionTwo];
    
    [[[AppDelegate shareAppDelegate] getCurrentUIVC] presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - 点击删除cell
- (void)deleteStudent:(UITapGestureRecognizer *)tap {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否删除选中的学生" preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        if (self.ClickDelete) {
            self.ClickDelete(self.Model.enrollInfoId,self.Model.User_ID);
            
        }
    }];
    
    UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
        if (_ReloadData) {
            _ReloadData();
            [deleteImage removeFromSuperview];
        }
    }];
    
    [alertVC addAction:actionOne];
    [alertVC addAction:actionTwo];
    
    [[[AppDelegate shareAppDelegate] getCurrentUIVC] presentViewController:alertVC animated:YES completion:nil];
}

@end
